package com.module.admin.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.module.admin.BaseController;
import com.module.admin.app.pojo.AppAnt;
import com.module.admin.app.service.AppAntService;
import com.module.admin.sys.pojo.SysUser;
import com.system.handle.model.ResponseCode;
import com.system.handle.model.ResponseFrame;

/**
 * 应用原型的Controller
 * @author yuejing
 * @date 2016-11-30 13:30:00
 * @version V1.0.0
 */
@Controller
public class AppAntController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(AppAntController.class);

	@Autowired
	private AppAntService appAntService;
	
	@RequestMapping(value = "/appAnt/f-view/edit")
	public String edit(HttpServletRequest request, ModelMap modelMap, String aaId) {
		if(aaId != null) {
			modelMap.put("appAnt", appAntService.get(aaId));
		}
		return "admin/app/appAnt-edit";
	}

	@RequestMapping(value = "/appAnt/f-json/findTree")
	@ResponseBody
	public void pageQuery(HttpServletRequest request, HttpServletResponse response,
			AppAnt appAnt) {
		ResponseFrame frame = new ResponseFrame();
		try {
			List<AppAnt> page = appAntService.findTree(appAnt);
			frame.setBody(page);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("分页获取信息异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}

	@RequestMapping(value = "/appAnt/f-json/save")
	@ResponseBody
	public void save(HttpServletRequest request, HttpServletResponse response,
			AppAnt appAnt) {
		ResponseFrame frame = null;
		try {
			SysUser user = getSessionUser(request);
			appAnt.setCreateUserId(user.getUserId());
			frame = appAntService.saveOrUpdate(appAnt);
		} catch (Exception e) {
			LOGGER.error("保存异常: " + e.getMessage(), e);
			frame = new ResponseFrame(ResponseCode.FAIL);
		}
		writerJson(response, frame);
	}
	
	@RequestMapping(value = "/appAnt/f-json/delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response,
			String aaId) {
		ResponseFrame frame = null;
		try {
			frame = appAntService.delete(aaId);
		} catch (Exception e) {
			LOGGER.error("删除异常: " + e.getMessage(), e);
			frame = new ResponseFrame();
			frame.setCode(ResponseCode.FAIL.getCode());
		}
		writerJson(response, frame);
	}
	
	@RequestMapping(value = "/appAnt/f-json/get")
	@ResponseBody
	public void get(HttpServletRequest request, HttpServletResponse response,
			String aaId) {
		ResponseFrame frame = new ResponseFrame();
		try {
			AppAnt data = appAntService.get(aaId);
			frame.setBody(data);
			frame.setSucc();
		} catch (Exception e) {
			LOGGER.error("异常: " + e.getMessage(), e);
			frame.setCode(ResponseCode.FAIL.getCode());
		}
		writerJson(response, frame);
	}

}